// File Name: ws2812b_ctrl.c
// Author: kukukukuku~
// Date: 2024-06-26

#include "./User/WS2812B/inc/ws2812b_ctrl.h"

WS2812B_RGB ws2812b[WS2812B_NUM];   //WS2812B对象

uint8_t ws2812bMode;        //WS2812B模式

uint8_t ws2812bState;       //WS2812B状态
uint8_t ws2812bCwMode;      //WS2812B正转时的RGB模式
uint8_t ws2812bCcwMode;     //WS2812B反转时的RGB模式

uint8_t ws2812bStopRed;     //WS2812B停止时红色值
uint8_t ws2812bStopGreen;   //WS2812B停止时绿色值
uint8_t ws2812bStopBlue;    //WS2812B停止时蓝色值

uint8_t ws2812bCwRed;       //WS2812B正转时红色值
uint8_t ws2812bCwGreen;     //WS2812B正转时绿色值
uint8_t ws2812bCwBlue;      //WS2812B正转时蓝色值

uint8_t ws2812bCcwRed;      //WS2812B反转时红色值
uint8_t ws2812bCcwGreen;    //WS2812B反转时绿色值
uint8_t ws2812bCcwBlue;     //WS2812B反转时蓝色值

/**
 * @brief   WS2812B初始化
 * @param   None
 * @retval  None
 * @note    静态函数
*/
static void ws2812b_init(void)
{
    gpio_init_type gpioInitStruct = 
    {
        .gpio_pins           = WS2812B_GPIO_PIN,
        .gpio_out_type       = GPIO_OUTPUT_PUSH_PULL,
        .gpio_pull           = GPIO_PULL_NONE,
        .gpio_mode           = GPIO_MODE_MUX,
        .gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
    };
    
    spi_init_type spiInitStruct = 
    {
        .transmission_mode      = SPI_TRANSMIT_HALF_DUPLEX_TX,
        .master_slave_mode      = SPI_MODE_MASTER,
        .mclk_freq_division     = SPI_MCLK_DIV_16,
        .first_bit_transmission = SPI_FIRST_BIT_MSB,
        .frame_bit_num          = SPI_FRAME_8BIT,
        .clock_polarity         = SPI_CLOCK_POLARITY_LOW,
        .clock_phase            = SPI_CLOCK_PHASE_2EDGE,
        .cs_mode_selection      = SPI_CS_SOFTWARE_MODE,
    };
    
    //WS2812B GPIO初始化
    crm_periph_clock_enable(WS2812B_GPIO_CRM, TRUE);
    gpio_init(WS2812B_GPIO_PORT, &gpioInitStruct);
    gpio_pin_mux_config(WS2812B_GPIO_PORT, WS2812B_GPIO_MUX_SOURCE, WS2812B_GPIO_MUX);
    
    //WS2812B SPI初始化
    crm_periph_clock_enable(WS2812B_SPI_CRM, TRUE);
    spi_init(WS2812B_SPI, &spiInitStruct);
    spi_enable(WS2812B_SPI, TRUE);
}


/**
 * @brief   WS2812B配置
 * @param   None
 * @retval  None
 * @note    None
*/
void ws2812b_config(void)
{
    ws2812b_init();
    
    ws2812bStopRed = 0x05;
    ws2812bStopGreen = 0x02;
    ws2812bStopBlue = 0x00;
    
    ws2812bCwRed = 0x05;
    ws2812bCwGreen = 0x00;
    ws2812bCwBlue = 0x00;
    
    ws2812bCcwRed = 0x00;
    ws2812bCcwGreen = 0x00;
    ws2812bCcwBlue = 0x10;

    ws2812bMode = WS2812B_MODE_FOLLOW_STEP_MOTOR;
}
